<?php

namespace App\Http\Controllers\Manage;

use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use App\Model\Manage;
use Hash;
use Validator;
//use App\Verifycode;
date_default_timezone_set('PRC');

class ManageController extends Controller
{
    //
    public function login() {
        return view('manage.login');
    }

    /**
     * 验证码
     */
    public function captcha() {
        $builder = new CaptchaBuilder();
        $builder->build($width = 86, $height = 34);
        Session::flash('captcha', $builder->getPhrase());
        header('Content-type:image/jpeg');
        $builder->output($quality = 80);
    }

    /**
     * 管理员登录验证
     */
    public function verify(Request $request) {
/*        $input = $request->only('email', 'password', 'captcha');
        dump($input);
        $results = Manage::all();
        foreach ($results as $result) {
            echo $result->email;
        }*/

        $input = $request->only('username', 'password', 'captcha');
        $callback = null;
        $rule = [
            'username' => 'required|',
            'password' => 'required|min:5',
            'captcha' => 'required|size:5'
        ];
        $message = [
            'username.required' => '邮箱不能为空',
            'password.required' => '密码不能为空',
            'password.min' => '密码长度最少 6 位',
            'captcha.required' => '验证码不能为空',
            'captcha.size' => '验证码长度 5 位'
        ];
        $validator = Validator::make($input, $rule, $message);
        if ($validator->fails()) {
            $callback = [
                'code' => 101,
                'msg' => '数据格式验证失败！'
            ];
        } elseif ($input['captcha'] != session('captcha')) {
            $callback = [
                'code' => 102,
                'msg' => '验证码错误！'
            ];
        }
        else {
            $result = Manage::where('username', $input['username'])
                ->where('status', 1)
                ->get();  //->first();
            if(count($result) == 0) {
                $callback = [
                    'code' => 104,
                    'msg' => '账号不存在'
                ];
            }
            foreach($result as $result) {
                if ($result && Hash::check($input['password'], $result->password)) {
                    Manage::where('username', $result['username'])
                        ->where('status', 1)
                        ->update([
                            'login_time' => date('Y-m-d H:i:s'),
                            'login_number' => $result['login_number'] + 1,
                            'ip' => $request->getClientIp()
                        ]);
                    $user = [
                        'id' => $result['id'],
                        'role_id' => $result['role_id'],
                        'name'=> $result['username']
                    ];
                    session(['user' => $user]);
                    $callback = [
                        'code' => 100,
                        'msg' => '登陆成功！'
                    ];
                } else {
                    $callback = [
                        'code' => 103,
                        'msg' => '用户名或密码错误'
                    ];
                }
            }

        }
        return json_encode($callback);
    }

    public function logout() {
        Session::flush();
        return redirect('manage/login');
    }

}
